<?php
namespace frontend\controllers;

use common\models\Apply;
use common\models\Article;
use common\models\Chat;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use dosamigos\qrcode\QrCode;
/**
 * Site controller
 */
class GrouthController extends Controller
{
    /**
     * @inheritdoc
     */
    public $layout='meno.php';
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],

            'access' => [
                'class' => AccessControl::className(),
                'only' => ['chat'],
                'rules' => [
                    [
                        'actions' => ['chat'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],

        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                'height' => 50,
                'width' => 80,
                'minLength' => 4,
                'maxLength' => 4
            ],
        ];
    }
    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        $this->layout="wide.php";
        $model=new Apply;
        $applied=false;
        if(!empty(Yii::$app->request->post())&&$model->load(Yii::$app->request->post())){
            $model->status=0;   //0表示无效  申请中 或者已经被使用
            $applied=true;

            if($model->save()) {
                Yii::$app->getSession()->setFlash('ok', '内测申请已提交！');
                return $this->render('index',['model'=>$model,'applied'=>$applied]);
            }
        }

        return $this->render('index',['model'=>$model,'applied'=>$applied]);
    }
    public  function actionAbout(){
        $content='';
        $article=Article::find()->where(['like','article_name',"公司资料"])->one();
        if(!empty($article))  $content=$article->content;
        return $this->render('about',['content'=>$content]);
    }
    public  function actionClick($name){
        $content='';
        $article=Article::find()->where(['like','article_name',$name])->one();
        if(!empty($article))  $content=$article->content;

        return $content;
    }
    public function actionChat($question){

        $uid=Yii::$app->user->id;
        $record=Chat::find()->where(['userId'=>$uid,'status'=>1])->orderBy('created_at DESC')->one();
        if(isset($record)&&(time()-$record->created_at<60)){
            echo "asked";
        }else {

            $model = new Chat();
            $model->userId = $uid;
            $model->content = $question;
            $model->parent_id = 0;
            $model->to = 0;
            $model->status = 1;
            $model->username = Yii::$app->user->identity->username;
            if ($model->save())
            {
                echo "yes";
            } else   {
                echo "no";
            }
        }

    }

}
